package com.millstein.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.millstein.commonutils.Result;
import com.millstein.eduservice.entity.EduCourse;
import com.millstein.eduservice.entity.EduTeacher;
import com.millstein.eduservice.entity.vo.CourseInfoVo;
import com.millstein.eduservice.entity.vo.CoursePublishVo;
import com.millstein.eduservice.entity.vo.CourseQuery;
import com.millstein.eduservice.entity.vo.TeacherQuery;
import com.millstein.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 添加课程的基本信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("id", id);
    }


    /**
     * 根据课程id查询其对应的基础信息
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable("courseId") String courseId) {
        CourseInfoVo vo = eduCourseService.getCourseInfo(courseId);
        return Result.ok().data("course", vo);
    }

    /**
     * 更新课程基础信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    /**
     * 课程发布前获取课程的确认信息
     * @param courseId
     * @return
     */
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public Result getPublishCourseInfo(@PathVariable("courseId") String courseId) {
        CoursePublishVo vo = eduCourseService.getPublishCourseInfo(courseId);
        return Result.ok().data("publishCourse", vo);
    }

    /**
     * 发布课程
     * @param courseId
     * @return
     */
    @PutMapping("/publishCourse/{courseId}")
    public Result publishCourse(@PathVariable("courseId") String courseId) {
        EduCourse course = new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        eduCourseService.updateById(course);
        return Result.ok();
    }

    /**
     * 分页条件查询课程信息
     * @param page 当前页
     * @param pageSize 每页条数
     * @param query 查询对象
     * @return
     */
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public Result pageCourseCondition(@PathVariable("current") Integer page,
                                       @PathVariable("limit") Integer pageSize,
                                       @RequestBody CourseQuery query) {
        Page<EduCourse> coursePage = new Page<>(page, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(query.getTitle())) {
            wrapper.like("title", query.getTitle());
        }
        if (query.getTeacherId() != null) {
            wrapper.eq("teacher_id", query.getTeacherId());
        }
        if (query.getSubjectParentId() != null) {
            wrapper.gt("subject_parent_id", query.getSubjectParentId());
        }
        if (query.getSubjectId() != null) {
            wrapper.lt("subject_id", query.getSubjectId());
        }
        eduCourseService.page(coursePage, wrapper);

        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        return Result.ok().data("total", total).data("rows", records);
    }


    /**
     * 删除课程信息
     * @param courseId
     * @return
     */
    @DeleteMapping("/{courseId}")
    public Result deleteCourse(@PathVariable("courseId") String courseId) {
        eduCourseService.deleteCourse(courseId);
        return Result.ok();
    }
}

